<template>
  <a-modal
    :title="isEdit ? '修改设备类型' : '新增设备类型'"
    :visible="show"
    @cancel="handleCancel"
    width="30%"
    class="procedure-modal"
  >
    <a-form-model
      ref="ruleForm"
      :model="item"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
      :rules="rules"
    >
      <a-form-model-item label="设备类型名称" prop="ProSegmentTypeName">
        <a-input v-model="item.ProSegmentTypeName" />
      </a-form-model-item>
      <a-form-model-item label="设备类型描述">
        <a-input v-model="item.ProSegmentTypeDesc" type="textarea" />
      </a-form-model-item>
    </a-form-model>
    <template slot="footer">
      <a-button :loading="loading" @click="handleCancel"> 返回 </a-button>
      <a-button type="primary" :loading="loading" @click="handleOk">
        提交
      </a-button>
    </template>
  </a-modal>
</template>
<script>
import {
  insProSegmentType,
  updProSegmentType,
} from "@/api/ProSegmentType/index";
export default {
  props: { show: Boolean, isEdit: Boolean, item: Object },
  data() {
    return {
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
      loading: false,
      rules: {
        ProSegmentTypeName: [
          {
            required: true,
            message: "请填写类型名称",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {},
  computed: {},
  methods: {
    //关闭弹窗
    handleCancel() {
      this.$emit("update:show", false);
      this.$refs.ruleForm.resetFields();
    },
    //确认添加/修改
    handleOk() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) {
          this.$message.error("请填写必填项");
          return;
        }

        this.loading = true;

        try {
          if (this.isEdit) {
            await updProSegmentType([this.item]);
            this.$message.success("修改成功");
          } else {
            await insProSegmentType([this.item]);
            this.$message.success("新增成功");
          }

          this.handleCancel();
          this.$emit("updata");
        } catch (e) {
          this.$message.error(e.message || "操作失败，请重试");
        } finally {
          this.loading = false;
        }
      });
    },
    /**  */
  },
};
</script>
<style scoped></style>
